import Vue from 'vue'
import VueRouter from 'vue-router'

import Layout from '@/views/layout'
import Login from '@/views/login'
import Myorder from '@/views/myorder'
import Pay from '@/views/pay'
import Prodetail from '@/views/prodetail'
import Search from '@/views/search'
import SearchList from '@/views/search/list.vue'
import Home from '@/views/layout/home'
import Catpage from '@/views/layout/catpage'
import Shopcart from '@/views/layout/shopcart'
import My from '@/views/layout/my'
import SidebarGoods from '@/components/SidebarGoods.vue'
import All from '@/views/myorder/All.vue'
import Dispatched from '@/views/myorder/Dispatched.vue'
import Evaluate from '@/views/myorder/Evaluate.vue'
import Receipt from '@/views/myorder/Receipt.vue'
import Unpaid from '@/views/myorder/Unpaid.vue'
import ListAll from '@/views/search/ListAll.vue'
import ListCount from '@/views/search/ListCount.vue'
import ListPrice from '@/views/search/ListPrice.vue'
import AddressList from '@/views/pay/AddressList.vue'
import NewAddress from '@/views/pay/NewAddress.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/layout' },
  {
    path: '/layout',
    component: Layout,
    children: [
      { path: '/layout', redirect: '/home' },
      { path: '/home', component: Home },
      {
        path: '/catpage',
        component: Catpage,
        children: [
          { path: '/catpage', redirect: '/hot' },
          { path: '/hot', component: SidebarGoods }
        ]
      },
      { path: '/shopcart', component: Shopcart },
      { path: '/my', component: My }

    ]
  },
  { path: '/login', component: Login },
  {
    path: '/myorder',
    component: Myorder,
    children: [
      { path: '/myorder', redirect: '/all' },
      { path: '/all', component: All },
      { path: '/dispatched', component: Dispatched },
      { path: '/evaluate', component: Evaluate },
      { path: '/unpaid', component: Unpaid },
      { path: '/receipt', component: Receipt }
    ]
  },
  { path: '/pay', component: Pay },
  { path: '/prodetail', component: Prodetail },
  { path: '/search', component: Search },
  {
    path: '/searchlist',
    component: SearchList,
    children: [
      { path: '/searchlist', redirect: '/listall' },
      { path: '/listall', component: ListAll },
      { path: '/listcount', component: ListCount },
      { path: '/listprice', component: ListPrice }
    ]
  },
  { path: '/addresslist', component: AddressList },
  { path: '/newaddress', component: NewAddress }
]
const router = new VueRouter({
  routes
})
// 创建导航守卫，在切换路由之前进行一个判断，如果没登录则进入登录页
const views = ['/myorder', '/pay', '/all', '/dispatched', '/evaluate', '/unpaid', 'receipt']
router.beforeEach((to, from, next) => {
  if (views.includes(to.path) && !localStorage.getItem('userinfo')) {
    next('/login')
  } else {
    next()
  }
})

export default router
